package com.koron.auth.service.impl;


import com.koron.bean.app.AppSimpleBean;
import com.koron.bean.system.org.OrgVo;
import com.koron.bean.system.staff.Constant;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.business.system.SystemCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("RemoteAuthServiceImpl")
@Slf4j
@RefreshScope
public class RemoteAuthServiceImpl {

	public boolean verify(String encryption, String account){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String dateString = sdf.format(date);
		String encryptionFirst = md5(account + dateString);
		String encryptionSecond = md5(encryptionFirst + "Koron&&Goolton");
		return encryption.equals(encryptionSecond);
	}

	public static String md5(String password) {
		MessageDigest md;
		try {
			md = MessageDigest.getInstance("MD5");//利用哈希算法，MD5
			//面向字节处理，所以可以处理字节的东西，如图片，压缩包。。
			byte[] input = password.getBytes();
			byte[] output = md.digest(input);
			//将md5处理后的output结果利用Base64转成原有的字符串,不会乱码
			String str = Base64.encodeBase64String(output);
			return str;
		} catch (NoSuchAlgorithmException e) {
			log.info("密码加密失败");
			return "";
		}
	}

	public Boolean defaultInfo(EamUser eamUser, HttpServletResponse response) {

		List<OrgVo> orgVoList = eamUser.getOrgVoList();
		List<AppSimpleBean> appSimpleBeanList = eamUser.getAppSimpleBeanList();

		if (CollectionUtils.isNotEmpty(orgVoList) && orgVoList.size() == 1
				&& CollectionUtils.isNotEmpty(appSimpleBeanList)
				//&& appSimpleBeanList.size() > 1){
				&& appSimpleBeanList.size() == 1) {

			//返回登录成功信息，前端可做跳转首页处理或者后台直接重定向到系统首页
			//cas认证通过，然后根据信息返回token
			String token = SystemCacheUtil.getNewToken(eamUser.getAccount(), eamUser.getName());

			List<String> userTokenList = new ArrayList<>();

			//redis拿出原来tokenList
			EamUser redisUser = SystemCacheUtil.getRedisUser(eamUser.getAccount());
			if (!Objects.isNull(redisUser) && CollectionUtils.isNotEmpty(redisUser.getTokenList())) {
				userTokenList = redisUser.getTokenList();
			}
			eamUser.setTokenList(userTokenList);
			SystemCacheUtil.cacheUserInfo(eamUser, token);

			if (response != null) {
				response.setHeader(Constant.JWT_TOKEN, token);
			}
			//附加上水司+组织
			StringBuilder sb = new StringBuilder();
			sb.append(token);
			sb.append("&currDs="+appSimpleBeanList.get(0).getAppMark());
			sb.append("&currOrg="+orgVoList.get(0).getId());
			response.setHeader("currDs", appSimpleBeanList.get(0).getAppMark());
			response.setHeader("currOrg", orgVoList.get(0).getId());
			return true;
		}
		return false;
	}
}
